
package com.ifcar.app.util;

import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ifcar.app.bean.PhotosetListInfo;
import com.ifcar.app.activity.R;

public class PictureGridAdapter extends BaseAdapter {
    public static final String FLAG = "picture";

    private Context mContext;

    private List<PhotosetListInfo> mList;

    private LayoutInflater mInflater;

    private BaseImageDownloader mImageDownloader = null;

    private PhotosetListInfo[] mInfosItem = new PhotosetListInfo[2];

//    private OnClickListener mClickListener;
    
    private int mImageWidth;
    private int mImageHeight;

    public PictureGridAdapter(Context c, List<PhotosetListInfo> list,
            BaseImageDownloader downloader, OnClickListener l) {
        super();
        mContext = c;
        mImageDownloader = downloader;
        mList = list;
//        mClickListener = l;
        float density = c.getResources().getDisplayMetrics().density;
		mImageWidth = (int) (140 * density);
		mImageHeight = (int) (147 * density);
        mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public void setOnClickListener(OnClickListener l) {
//        mClickListener = l;
    }

    @Override
    public int getCount() {
        if (mList == null || mList.size() == 0) {
            return 0;
        }

        return (mList.size() + 1) / 2;
    }

    @Override
    public PhotosetListInfo[] getItem(int index) {
        index += index;
        if (index >= mList.size()) {
            mInfosItem[0] = null;
        } else {
            mInfosItem[0] = mList.get(index);
        }
        index++;
        if (index >= mList.size()) {
            mInfosItem[1] = null;
        } else {
            mInfosItem[1] = mList.get(index);
        }
        return mInfosItem;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

    int i = 0;

    /**
     * 图集填充
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.picture_set_item, parent, false);
            holder.itemHolder1.item = convertView.findViewById(R.id.picture_item1);
//            holder.itemHolder1.item.setOnClickListener(mClickListener);
            holder.itemHolder1.image = (ImageView) holder.itemHolder1.item
                    .findViewById(R.id.photosetitemimage);
            holder.itemHolder1.name = (TextView) holder.itemHolder1.item
                    .findViewById(R.id.photosetitemtext);
            holder.itemHolder2.item = convertView.findViewById(R.id.picture_item2);
//            holder.itemHolder2.item.setOnClickListener(mClickListener);
            holder.itemHolder2.image = (ImageView) holder.itemHolder2.item
                    .findViewById(R.id.photosetitemimage);
            holder.itemHolder2.name = (TextView) holder.itemHolder2.item
                    .findViewById(R.id.photosetitemtext);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        PhotosetListInfo[] infos = getItem(position);
        fillData(infos[0], holder.itemHolder1);
        fillData(infos[1], holder.itemHolder2);

        return convertView;
    }

    private void fillData(PhotosetListInfo info, ItemViewHolder holder) {
        holder.item.setVisibility(View.INVISIBLE);
//        holder.item.setTag(info);
        
        if (info == null) {
            return;
        } else if ("0".equals(info.getSetid())) {
            holder.item.setVisibility(View.VISIBLE);
            holder.name.setText("");
            holder.image.setImageResource(R.drawable.picture_more);
        } else {
            holder.item.setVisibility(View.VISIBLE);
            holder.name.setText(info.getSetname());
            holder.image.setImageDrawable(null);

			int width = mImageWidth - holder.image.getPaddingLeft()
					- holder.image.getPaddingRight();
			int height = mImageHeight - holder.image.getPaddingTop()
					- holder.image.getPaddingBottom();
			mImageDownloader.downloadPictures(info.getClientcover(), holder.image, width, height, "Y",
                    null, FLAG);        }

    }

    private class ViewHolder {
        ItemViewHolder itemHolder1 = new ItemViewHolder();

        ItemViewHolder itemHolder2 = new ItemViewHolder();
    }

    private class ItemViewHolder {
        View item;

        ImageView image;

        TextView name;
    }
}
